<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Member extends CI_Controller {

    public function index() {
        if (!$this->session->userdata('sessionMember'))
            redirect(base_url() . 'memberlogin');

        $sesMember = $this->session->userdata('sessionMember');

        $this->load->model("register_model");
        $check = $this->register_model->checkMember($sesMember);

        switch ($check['status']) {
            case "0":
                redirect(base_url() . 'memberlogin');
                break;
            case "1":
                redirect(base_url() . 'member/profile');
                break;
            case "2":
            	$this->_dashboard();
                break;
        }
    }

    public function login() {
        if ($this->input->post()) {
            $data['uid'] = $this->input->post('username', TRUE);
            $data['pin'] = $this->input->post('password', TRUE);
            $this->load->model("register_model");
            $members = $this->register_model->getUserLogin($data);
            $login['sessionMember'] = $members['uid'];
            $this->session->set_userdata($login);
            redirect(base_url() . "member/index");
        } else {
            redirect(site_url('memberlogin'));
        }
    }

    public function logout() {
        $login['sessionMember'] = false;
        $this->session->unset_userdata($login);
        redirect(site_url('memberlogin'));
    }

    public function profile() {
        if (!$this->session->userdata('sessionMember'))
            redirect(base_url() . 'memberlogin');

        $sesMember = $this->session->userdata('sessionMember');
        $this->load->model("register_model");

        $this->form_validation->set_rules('gender', 'gender', '');
        $this->form_validation->set_rules('ktp', 'ktp', '');
        $this->form_validation->set_rules('name', 'name', '');
        $this->form_validation->set_rules('birth_date', 'birth_date', '');
        $this->form_validation->set_rules('birth_place', 'birth_place', '');
        $this->form_validation->set_rules('address', 'address', '');
        $this->form_validation->set_rules('postal_code', 'postal_code', '');
        $this->form_validation->set_rules('province', 'province', '');
        $this->form_validation->set_rules('mother_name', 'mother_name', '');
        $this->form_validation->set_rules('phone', 'phone', '');
        $this->form_validation->set_rules('bank_name', 'bank_name', '');
        $this->form_validation->set_rules('bank_branch', 'bank_branch', '');
        $this->form_validation->set_rules('bank_account', 'bank_account', '');
        $this->form_validation->set_rules('bank_username', 'bank_username', '');
        $this->form_validation->set_rules('heir_name', 'heir_name', '');
        $this->form_validation->set_rules('heir_relation', 'heir_relation', '');
        $this->form_validation->set_rules('pin', 'Password', 'required|matches[repin]|min_length[6]|alpha_numeric');
        $this->form_validation->set_rules('repin', 'Konfirmasi', 'required');
        $this->form_validation->set_message('required', 'Field %s wajib di isi.');
        $this->form_validation->set_message('min_length', 'Password Minimal 6 Karakter');
        $this->form_validation->set_message('alpha_numeric', 'Password harus berupa alpanumerik');
        $this->form_validation->set_message('matches', '%s tidak sama dengan konfirmasi.');

        if ($this->input->post() && $this->form_validation->run()) {
            $data['uid']			= $sesMember;
            $data['gender']			= $this->input->post('gender', TRUE);
            $data['ktp']			= $this->input->post('ktp', TRUE);
            $data['name']			= $this->input->post('name', TRUE);
            $data['birth_date']		= $this->input->post('birth_date', TRUE);
            $data['birth_place']	= $this->input->post('birth_place', TRUE);
            $data['email']			= $this->input->post('email', TRUE);
            $data['address']		= $this->input->post('address', TRUE);
            $data['postal_code']	= $this->input->post('postal_code', TRUE);
            $data['province']		= $this->input->post('province', TRUE);
            $data['mother_name']	= $this->input->post('mother_name', TRUE);
            $data['phone']			= $this->input->post('phone', TRUE);
            $data['bank_name']		= $this->input->post('bank_name', TRUE);
            $data['bank_branch']	= $this->input->post('bank_branch', TRUE);
            $data['bank_account']	= $this->input->post('bank_account', TRUE);
            $data['bank_username']	= $this->input->post('bank_username', TRUE);
            $data['heir_name']		= $this->input->post('heir_name', TRUE);
            $data['heir_relation']	= $this->input->post('heir_relation', TRUE);
            $data['pin']			= $this->input->post('pin', TRUE);

            if ($this->register_model->updateMemberById($data))
                redirect(base_url() . "member/index");
        }

        $data = array();
        $data['member'] = $this->register_model->getMemberById($sesMember);

        $data['template']['nav']		= "essential/member_header";
        $data['template']['header']		= "member/header";
        $data['template']['content']	= "member/update";
        $data['template']['nosidebar']	= TRUE;

        $this->load->view('essential/template', $data);
    }
    
    private function _dashboard() {
    	
    	$sesMember	= $this->session->userdata('sessionMember');
    	$data		= array ();
    	
    	$this->load->model('tree_model');
    	
    	$data['member']		= $this->tree_model->getNode($sesMember);
    	$data['comission']	= $this->tree_model->countComission($data['member']);
    	$data['child']		= $this->tree_model->countChild($data['member']);
    	
    	$data['member']['limit']	= 10;
    	$data['member']['offset']	= ($data['child'] < $data['member']['limit'] ) ?
    									0 : $data['child'] - $data['member']['limit'];  
    	
    	$data['list']		= $this->tree_model->getChild($data['member']);
    	
		$data ['template'] ['nav'] = "essential/member_header";
		$data ['template'] ['content'] = 'member/index';
		$data ['template'] ['nosidebar'] = TRUE;
		
		$this->load->view ( 'essential/template', $data );
    }

}